/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.time;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.logging.Level;

import util.LoggingUtils;

/**
 * 
 * @author Himadri Talukder <himadri@digitalmenubox.com>
 */
public class PlaylistRecordRestriction {

	private Calendar	fromTime;
	private Calendar	toTime;
	private String[]	days;

	public PlaylistRecordRestriction(Calendar from, Calendar to, String[] days) {
		// assert (from.compareTo(to) < 0) : from.getTime() + " " +
		// to.getTime();
		this.fromTime = from;
		this.toTime = to;
		this.days = days;
		this.fromTime.set(Calendar.MILLISECOND, 0);
		this.toTime.set(Calendar.MILLISECOND, 0);
		Arrays.sort(days);
	}

	public Calendar getToTime() {
		return this.toTime;
	}

	public Calendar getFromTime() {
		return this.fromTime;
	}

	/**
	 * @precondition this.fromTime!=null && this.toTime!=null
	 * @postcondition
	 * @return
	 */
	public boolean isPlayableNow() {

		if (this.toTime.before(this.fromTime)) {
			System.out.println(this.fromTime.getTime().toString() + " " + this.toTime.getTime().toString());
			return false;
		}
		Calendar now = Calendar.getInstance();
		
		if (now.before(this.toTime)
				&& now.after(this.fromTime)
				&& 0 <= Arrays.binarySearch(this.days,
						now.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.ENGLISH))) {

			return true;
		}

		return false;
		
	}
	
	public boolean isPlayableNow(String recurring) {
		
		Calendar now = Calendar.getInstance();
		int hour = now.get(Calendar.HOUR_OF_DAY);
        int minute = now.get(Calendar.MINUTE);
        int second = now.get(Calendar.SECOND);

        int nowSecond = second + 60 * minute + 3600 * hour;						//time of day in second
        int startSecond = this.fromTime.get(Calendar.HOUR_OF_DAY) * 3600 + this.fromTime.get(Calendar.MINUTE) * 60 + this.fromTime.get(Calendar.SECOND);
        int endSecond = this.toTime.get(Calendar.HOUR_OF_DAY) * 3600 + this.toTime.get(Calendar.MINUTE) * 60 + this.toTime.get(Calendar.SECOND);
        
        System.out.println("Start = "+startSecond+", Now = "+nowSecond+", endSecond = "+endSecond);
		
		this.toTime.set(this.toTime.get(Calendar.YEAR),this.toTime.get(Calendar.MONTH),this.toTime.get(Calendar.DAY_OF_MONTH),23,59,59);
		this.fromTime.set(this.fromTime.get(Calendar.YEAR),this.fromTime.get(Calendar.MONTH),this.fromTime.get(Calendar.DAY_OF_MONTH),0,0,0);
		
		System.out.println(this.toTime.getTime());
		System.out.println(this.fromTime.getTime());
		
		if (this.toTime.before(this.fromTime)) {
			System.out.println(this.fromTime.getTime().toString() + " " + this.toTime.getTime().toString());
			return false;
		}
				
		if (now.before(this.toTime)
				&& now.after(this.fromTime)
				&& recurringPlayable(startSecond, nowSecond, endSecond)
				&& 0 <= Arrays.binarySearch(this.days,
						now.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.ENGLISH))) {
			return true;
		}
		return false;
	}
	
	public boolean recurringPlayable(int startSecond, int nowSecond, int endSecond)
	{
		if (startSecond <= nowSecond && endSecond >= nowSecond) {
            return true;
        } else {
            return false;
        }
	}

	@Override
	public String toString() {
		// from Time
		// int fromYear = this.fromTime.get(Calendar.YEAR);
		// int fromMonth = this.fromTime.get(Calendar.MONTH);
		// int fromDay = this.fromTime.get(Calendar.DAY_OF_MONTH);
		// int fromHour = this.fromTime.get(Calendar.HOUR_OF_DAY);
		// int fromMinute = this.fromTime.get(Calendar.MINUTE);
		// int fromSecond = this.fromTime.get(Calendar.SECOND);
		// // to Time
		// int toYear = this.toTime.get(Calendar.YEAR);
		// int toMonth = this.toTime.get(Calendar.MONTH);
		// int toDay = this.toTime.get(Calendar.DAY_OF_MONTH);
		// int toHour = this.toTime.get(Calendar.HOUR_OF_DAY);
		// int toMinute = this.toTime.get(Calendar.MINUTE);
		// int toSecond = this.toTime.get(Calendar.SECOND);
		DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
		return String.format("%s - %s", df.format(this.fromTime.getTime()), df.format(this.toTime.getTime()));
		// return
		// String.format("Date: %1$d/%2$d/%3$d - %4$d/%5$d/%6$d Time: %7$d:%8$d:%9$d - %10$d:%11$d:%12$d",
		// fromYear, fromMonth, fromDay, toYear, toMonth, toDay, fromHour,
		// fromMinute, fromSecond, toHour, toMinute, toSecond);
	}
	
	public static void main(String[] args) {
		/*
		int from = PlaylistRecordRestriction.timeTosec("10:20:10");
		int mid = PlaylistRecordRestriction.timeTosec("13:20:09");
		int to = PlaylistRecordRestriction.timeTosec("13:20:10");
	
		if(mid > from && mid < to)
		{
			System.out.println("True");
		}
		else
		{
			System.out.println("False");
		}
		
		
		Calendar f = Calendar.getInstance();
		Calendar t = Calendar.getInstance();
		f.set(2012, 04, 22, 10, 0, 0);
		t.set(2012, 04, 31, 17, 0, 0);
		// System.out.println(f.getTimeInMillis() + " " + t.getTimeInMillis());
		// System.out.println(f.getTime() + " " + t.getTime());
		PlaylistRecordRestriction r = new PlaylistRecordRestriction(f, t, new String[] { "Monday", "Thursday" });
		System.out.println(r.isPlayableNow());
		System.out.println(r);
		*/
		
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date from = null;
		Date to = null;
		
		try {
			from = formatter.parse("2012-04-03 02:00:00");
			to = formatter.parse("2030-01-01 23:00:00");
			Calendar calFrom = Calendar.getInstance();
			Calendar calTo = Calendar.getInstance();
			calFrom.setTime(from);
			calTo.setTime(to);
			
			Calendar f = Calendar.getInstance();
			Calendar t = Calendar.getInstance();
			f.setTime(from);
			t.setTime(to);
			
			PlaylistRecordRestriction r = new PlaylistRecordRestriction(f, t, new String[] { "Monday", "Wednesday" });
			System.out.println(r.isPlayableNow(""));
			
		} catch (ParseException e) {
			LoggingUtils.log(Level.SEVERE,e.getMessage(),e);
		}
		catch(NullPointerException nu)
		{
			LoggingUtils.log(Level.SEVERE,nu.getMessage(),nu);
		}
		
	}

	public boolean isExpired() {
		return (this.toTime.compareTo(Calendar.getInstance())<0);
	}
}